﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Globalization;
using Microsoft.Win32;
using System.Windows.Forms;

namespace CemaatVakti
{
    public static class Helpers
    {
        public static string MD5(string password)
        {
            byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
            //try
            //{
            System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
            cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hash = cryptHandler.ComputeHash(textBytes);
            string ret = "";
            foreach (byte a in hash)
            {
                if (a < 16)
                    ret += "0" + a.ToString("x");
                else
                    ret += a.ToString("x");
            }
            return ret;
        }

        public static ArrayList parseNamazVakitleri()
        {
            ArrayList Vakitler = new ArrayList();

            HTTPWorker http = new HTTPWorker();
            HttpWebResponse rsp = null;
            http.Url = Helpers.getSetting("NamazURL");
            http.Type = HTTPRequestType.Get;
            try
            {
                rsp = http.SendRequest();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }

            if (rsp.StatusCode == HttpStatusCode.OK)
            {
                String responseStr = http.ResponseText;
                if (Helpers.isDataValid(responseStr))
                {
                    String[] vakitler = responseStr.Split('#');
                    Vakit sabahVakti = new Vakit("İmsak", (DateTime.Parse(vakitler[0])), true);
                    Vakitler.Add(sabahVakti);
                    Vakit gunesVakti = new Vakit("Güneş", (DateTime.Parse(vakitler[1])), false);
                    Vakitler.Add(gunesVakti);
                    Vakit ogleVakti = new Vakit("Öğle", (DateTime.Parse(vakitler[2])), true);
                    Vakitler.Add(ogleVakti);
                    Vakit ikindiVakti = new Vakit("İkindi", (DateTime.Parse(vakitler[3])), true);
                    Vakitler.Add(ikindiVakti);
                    Vakit aksamVakti = new Vakit("Akşam", (DateTime.Parse(vakitler[4])), true);
                    Vakitler.Add(aksamVakti);
                    Vakit yatsiVakti = new Vakit("Yatsı", (DateTime.Parse(vakitler[5])), true);
                    Vakitler.Add(yatsiVakti);
                    return Vakitler;
                }
                else
                {

                }

                return null;
            }
            else if (rsp.StatusCode != HttpStatusCode.Found)
            {
                Console.WriteLine("Got unexpected status code '{0}' when trying to login.", rsp.StatusCode);
                return null;
            }
            else
            {
                return null;
            }
        }

        public static Boolean HasUpdates()
        {
            HTTPWorker http = new HTTPWorker();
            HttpWebResponse rsp = null;
            http.Url = Globals.UpdateURL;
            http.Type = HTTPRequestType.Get;
            try
            {
                rsp = http.SendRequest();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

            if (rsp.StatusCode == HttpStatusCode.OK)
            {
                String responseStr = http.ResponseText;
                if(!responseStr.Equals(Globals.version))
                {
                    return true;
                }
                return false;
            }
            else if (rsp.StatusCode != HttpStatusCode.Found)
            {
                Console.WriteLine("Got unexpected status code '{0}' when trying to login.", rsp.StatusCode);
                return false;
            }
            else
            {
                return false;
            }
        }

        public static DateTime sunucuSaatiniGetir()
        {
            Hashtable NamazVakitleri = new Hashtable();

            HTTPWorker http = new HTTPWorker();
            HttpWebResponse rsp = null;
            http.Url = Globals.NamazURL + "?tarih=true";
            http.Type = HTTPRequestType.Get;
            try
            {
                rsp = http.SendRequest();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                return DateTime.Now;
            }

            if (rsp.StatusCode == HttpStatusCode.OK)
            {
                String responseStr = http.ResponseText;
                CultureInfo MyCultureInfo = new CultureInfo("tr-TR");
                return DateTime.Parse(responseStr, MyCultureInfo);

                //düzgün parset etmem gerekli
            }
            else if (rsp.StatusCode != HttpStatusCode.Found)
            {
                Console.WriteLine("Got unexpected status code '{0}' when trying to login.", rsp.StatusCode);
                return DateTime.Now;
            }
            else
            {
                return DateTime.Now;
            }
        }

        public static Boolean isDataValid(String str)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, "([0-9]+[:][0-9]+[#])+");
        }

        public static String getSetting(String key)
        {
            NameValueCollection appSettings = ConfigurationSettings.AppSettings;
            return appSettings.Get(key);
        }

        public static void writeSetting(String key, String value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[key].Value = value;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static void AcilistaCalistir()
        {
            RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (rkApp.GetValue("CemaatVakti") == null)
            {
                rkApp.SetValue("CemaatVakti", Application.ExecutablePath.ToString());
            }
        }

        public static void AcilistaCalistirma()
        {
            RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (rkApp.GetValue("CemaatVakti") != null)
            {
                rkApp.DeleteValue("CemaatVakti", false);
            }
        }
    }
}
